<?php

namespace App\Http\Controllers\Admin;


use App\Exceptions\AdminException;
use App\Http\Controllers\AdminBaseController;
use App\Http\Requests\Admin\LoginRequest;
use App\Services\Admin\IndexService;
use App\Util\LoginUtil;

class LoginController extends AdminBaseController
{
    protected $services = null;


    public function __construct(IndexService $Services)
    {
        parent::__construct();
        $this->services = $Services;
    }


    public function captcha()
    {
        return $this->success(array([
            'status_code' => '200',
            'message' => 'created succeed',
            'url' => app('captcha')->create('default', true)
        ]));
    }


    public function login()
    {
        return view('admin.login');
    }

    //注册
    public function register(LoginRequest $request)
    {
        return $this->success($this->services->register($request));
    }


    //登入
    public function dologin(LoginRequest $request)
    {
        $key = $request->input('key'); //key
        return $this->success($this->services->login($request));
    }



    /**
     * 注销登录
     *
     * @return \Illuminate\Http\Response
     */
    public function logout()
    {
        try {
            LoginUtil::logout();
        } catch (\Exception $e) {
        }
        return $this->success();
    }

    /**
     * 注销登录
     *
     * @return \Illuminate\Http\Response
     */
    public function logoutredirect()
    {
        try {
            LoginUtil::logout();
        } catch (\Exception $e) {
        }
        return redirect('/api/admin/login');
    }
}
